package com.only4play.demo.imserve.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.only4play.demo.imserve.model.UserContext;
import com.only4play.demo.imserve.service.IUserService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Optional;

import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;

/**
 * 用户认证handler
 */
@Slf4j
@ChannelHandler.Sharable
public class UserAuthHandler extends ChannelInboundHandlerAdapter {

  public static final AttributeKey<UserContext> USER_CONTEXT_ATTRIBUTE_KEY =
      AttributeKey.valueOf("USER_CONTEXT_ATTRIBUTE_KEY");

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg)
      throws Exception {
    // 表示HTTP请求
    if (msg instanceof FullHttpMessage) {
      HttpHeaders headers = ((FullHttpMessage) msg).headers();
      String token = Objects.requireNonNull(headers.get("token"));
      //获取token，登录之后才能连接
      IUserService userService = SpringUtil.getBean(IUserService.class);
      Optional<UserContext> userContext = userService.parseToken(token);
      if (userContext.isPresent()) {
        // 通道与用户信息绑定
        ctx.channel().attr(USER_CONTEXT_ATTRIBUTE_KEY).set(userContext.get());
        ctx.fireUserEventTriggered(userContext.get());
      } else {
        DefaultFullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
        sendHttpResponse(ctx, (FullHttpRequest) msg, response);
      }
    }
    super.channelRead(ctx, msg);
  }

  private static void sendHttpResponse(ChannelHandlerContext ctx,
      FullHttpRequest req, DefaultFullHttpResponse res) {
    if (res.status().code() != HttpResponseStatus.OK.code()) {
      ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(),
          StandardCharsets.UTF_8);
      res.content().writeBytes(buf);
      buf.release();
    }
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!isKeepAlive(req) || res.status().code() != HttpResponseStatus.OK.code()) {
      f.addListener(ChannelFutureListener.CLOSE);
    }
  }
}
